<template>
	<view :style="colorStyle">
		<view class="my-order">
			<!-- 顶部统计 -->
			<view class="header bg-color">
				<view class="picTxt acea-row row-between-wrapper">
					<view class="text">
						<view class="name">{{$t(`订单信息`)}}</view>
					</view>
				</view>
			</view>
			<!-- Tab 选项 -->
			<view class="nav acea-row row-around">
				<view class="item" :class="orderStatus === undefined ? 'on' : ''" @click="statusClick()">
					<view>{{$t(`全部`)}}</view>
					<view class="num">{{ orderData.allCount || 0 }}</view>
				</view>
				<view class="item" :class="orderStatus === 0 ? 'on' : ''" @click="statusClick(0)">
					<view>{{$t(`待付款`)}}</view>
					<view class="num">{{ orderData.unpaidCount || 0 }}</view>
				</view>
				<view class="item" :class="orderStatus === 10 ? 'on' : ''" @click="statusClick(10)">
					<view>{{$t(`待发货`)}}</view>
					<view class="num">{{ orderData.undeliveredCount || 0 }}</view>
				</view>
				<view class="item" :class="orderStatus === 20 ? 'on' : ''" @click="statusClick(20)">
					<view>{{$t(`待收货`)}}</view>
					<view class="num ">{{ orderData.deliveredCount || 0 }}</view>
				</view>
				<view class="item" :class="orderStatus === 30 ? 'on' : ''" @click="statusClick(30, false)">
					<view>{{$t(`待评价`)}}</view>
					<view class="num">{{ orderData.uncommentedCount || 0 }}</view>
				</view>
			</view>
			<!-- 订单列表 -->
			<view class="list">
				<view class="item" v-for="(item, index) in orderList" :key="index">
					<view @click="goOrderDetails(item.order_id)">
						<!-- 订单信息 -->
            <view class="title acea-row row-between-wrapper">
							<view class="acea-row row-middle">
								<text class="sign cart-color acea-row row-center-wrapper"
									v-if="item.type === 2">{{$t(`砍价`)}}</text>
								<text class="sign cart-color acea-row row-center-wrapper"
									v-else-if="item.type === 3">{{$t(`拼团`)}}</text>
								<text class="sign cart-color acea-row row-center-wrapper"
									v-else-if="item.type === 1">{{$t(`秒杀`)}}</text>
								<text class="sign cart-color acea-row row-center-wrapper"
									v-else-if="item.type === 4">{{$t(`预售`)}}</text>
								<view>{{ item.createTime | dateFormat }}</view>
							</view>
							<!-- 订单状态 -->
							<view v-if="item.status === 0" class="font-color">{{$t(`待付款`)}}</view>
							<view v-else-if="item.status === 10 && item.deliveryType === 1" class="font-color">{{$t(`待发货`)}}</view>
							<view v-else-if="item._status === 10 && item.deliveryType === 2" class="font-color">{{$t(`待核销`)}}</view>
							<view v-else-if="item.status === 20" class="font-color">{{$t(`待收货`)}}</view>
							<view v-else-if="item.status === 30 && !item.commentStatus" class="font-color">{{$t(`待评价`)}}</view>
							<view v-else-if="item.status === 30 && item.commentStatus" class="font-color">{{$t(`已完成`)}}</view>
              <!-- TODO 芋艿：item._status._type == 5 && item.status == 0 原判断 -->
							<view v-else-if="item.status === 5 && item.status === 0" class="font-color">{{$t(`未核销`)}}</view>
							<view v-else-if="item.status === 40" class="font-color">{{$t(`已关闭`)}}</view>
						</view>
						<!-- 订单项信息 -->
            <view class="item-info acea-row row-between row-top" v-for="(items, indexCat) in item.items" :key="indexCat">
							<view class="pictrue">
								<easy-loadimage mode="widthFix" :image-src="items.picUrl"></easy-loadimage>
								<!-- <image :src="items.productInfo.image"></image> -->
							</view>
							<view class="text  row-between">
								<text class="name line2">{{ items.spuName }}</text>
								<view class="money">
									<view>
										{{$t(`￥`)}}{{ (items.price / 100.0).toFixed(2) }}
									</view>
									<view>x{{ items.count }}</view>
									<!-- TODO 芋艿：退款 -->
									<view v-if="items.refund_num && item.status !== -2" class="return">
										{{ items.refund_num }}{{$t(`件退款中`)}}
									</view>
								</view>
							</view>
						</view>
						<!-- 订单金额 -->
            <view class="totalPrice">
							{{$t(`共`)}}{{ item.productCount || 0 }}{{$t(`件商品，总金额`)}}
							<text class="money">{{$t(`￥`)}}{{ (item.payPrice / 100.0).toFixed(2) }}</text>
						</view>
					</view>
          <!-- 订单操作区 -->
					<view class="bottom acea-row row-right row-middle">
						<view class="bnt cancelBnt" v-if="item.status === 0"
							@click="cancelOrder(index, item.order_id)">{{$t(`取消订单`)}}</view>
						<view class="bnt bg-color" v-if="item.status === 0"
							@click="goPay(item.pay_price, item.order_id)">{{$t(`立即付款`)}}</view>
						<!-- <view class="bnt bg-color" v-else-if="item._status._type == 3"
							@click="goOrderDetails(item.order_id)">去评价</view> -->
						<!-- <view class="bnt bg-color"
							v-else-if="item.seckill_id < 1 && item.bargain_id < 1 && item.combination_id < 1 && item._status._type == 4"
							@click="goOrderDetails(item.order_id)">
							再次购买
						</view> -->
						<view class="bnt cancelBnt" v-if="item.status === 40"
							@click="delOrder(item.order_id, index)">{{$t(`删除订单`)}}</view>
						<view class="bnt bg-color" @click="goOrderDetails(item.id)">{{$t(`查看详情`)}}</view>
					</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view class="loadingicon acea-row row-center-wrapper" v-if="orderList.length > 0">
				<text class="loading iconfont icon-jiazai" :hidden="loading === false"></text>
				{{ loadTitle }}
			</view>
			<view v-if="orderList.length === 0">
				<emptyPage v-if="!loading" :title="$t(`暂无订单`)"></emptyPage>
				<view class="loadingicon acea-row row-center-wrapper">
					<text class="loading iconfont icon-jiazai" :hidden="loading === false"></text>
				</view>
			</view>
		</view>
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
	</view>
</template>
<script>
	import { orderCancel,  orderDel } from '@/api/order.js';
  import * as TradeOrderApi from '@/api/trade/order.js';
	import { openOrderSubscribe } from '@/utils/SubscribeMessage.js';
	import home from '@/components/home';
	import { toLogin } from '@/libs/login.js';
	import { mapGetters } from 'vuex';
  import dayjs from '@/plugin/dayjs/dayjs.min.js';
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import emptyPage from '@/components/emptyPage.vue';
	import colors from '@/mixins/color.js';
	export default {
		components: {
			home,
			emptyPage,
			// #ifdef MP
			authorize
			// #endif
		},
    filters: {
      dateFormat: function(value) {
        return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
      }
    },
		mixins: [colors],
		data() {
			return {
				loading: false, //是否加载中
				loadend: false, //是否加载完毕
				loadTitle: this.$t(`加载更多`), //提示语
				orderList: [], //订单数组
				orderData: {}, //订单详细统计
				orderStatus: undefined, // Tab 的订单状态
				commentStatus: undefined, // Tab 的评论状态
				page: 1,
				limit: 20,
				initIn: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false //是否隐藏授权
			};
		},
		computed: mapGetters(['isLogin']),
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			if (options.status) this.orderStatus = options.status;
			if (this.isLogin) {
				this.page = 1;
				this.orderList = []
				this.loadend = false;
				this.onLoadFun();
				this.getOrderList();
			} else {
				toLogin();
			}
		},
		methods: {
			onLoadFun() {
				this.getOrderData();
			},

			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e;
			},

			/**
			 * 获取订单统计数据
			 */
			getOrderData: function() {
				let that = this;
        TradeOrderApi.getOrderCount().then(res => {
					that.$set(that, 'orderData', res.data);
				});
			},

			/**
			 * 取消订单
			 */
			cancelOrder: function(index, order_id) {
				let that = this;
				if (!order_id)
					return that.$util.Tips({
						title: that.$t(`缺少订单号无法取消订单`)
					});
				orderCancel(order_id)
					.then(res => {
						return that.$util.Tips({
								title: res.msg,
								icon: 'success'
							},
							function() {
								that.orderList.splice(index, 1);
								that.$set(that, 'orderList', that.orderList);
								that.$set(that.orderData, 'unpaidCount', that.orderData.unpaidCount - 1);
								that.getOrderData();
							}
						);
					}).catch(err => {
						return that.$util.Tips({
							title: err
						});
					});
			},
			/**
			 * 打开支付组件
			 */
			goPay: function(pay_price, order_id) {
				uni.navigateTo({
					url: `/pages/goods/cashier/index?order_id=${order_id}&from_type=order`
				})
			},
			/**
			 * 去订单详情
			 */
			goOrderDetails: function(order_id) {
				let that = this;
				if (!order_id)
					return that.$util.Tips({
						title: that.$t(`缺少订单号无法查看订单详情`)
					});
				// #ifdef MP
				uni.showLoading({
					title: that.$t(`正在加载中`)
				});
				openOrderSubscribe()
					.then(() => {
						uni.hideLoading();
						uni.navigateTo({
							url: '/pages/goods/order_details/index?order_id=' + order_id
						})
					})
					.catch(err => {
						uni.hideLoading();
					});
				// #endif
				// #ifndef MP
				uni.navigateTo({
					url: '/pages/goods/order_details/index?order_id=' + order_id
				});
				// #endif
			},
			/**
			 * 切换类型
			 */
			statusClick: function(status, commentStatus) {
				if (status === this.orderStatus) return;
				this.orderStatus = status;
				this.commentStatus = commentStatus;
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.getOrderList();
			},
			/**
			 * 获取订单列表
			 */
			getOrderList: function() {
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				that.loading = true;
				that.loadTitle = that.$t(`加载更多`);
				TradeOrderApi.getOrderPage({
				  status: that.orderStatus,
				  commentStatus: that.commentStatus,
				  pageNo: that.page,
				  pageSize: that.limit
				}).then(res => {
				  let list = res.data.list || [];
				  let loadend = list.length < that.limit;
				  that.orderList = that.$util.SplitArray(list, that.orderList);
				  that.$set(that, 'orderList', that.orderList);
				  that.loadend = loadend;
				  that.loading = false;
				  that.loadTitle = loadend ? that.$t(`没有更多内容啦~`) : that.$t(`加载更多`);
				  that.page = that.page + 1;
				}).catch(err => {
				  that.loading = false;
				  that.loadTitle = that.$t(`加载更多`);
				});
			},

			/**
			 * 删除订单
			 */
			delOrder: function(order_id, index) {
				let that = this;
				uni.showModal({
					title: that.$t(`删除订单`),
					content: that.$t(`确定删除该订单`),
					success: function(res) {
						if (res.confirm) {
							orderDel(order_id)
								.then(res => {
									that.orderList.splice(index, 1);
									that.$set(that, 'orderList', that.orderList);
									that.$set(that.orderData, 'unpaid_count', that.orderData
										.unpaid_count - 1);
									that.getOrderData();
									return that.$util.Tips({
										title: that.$t(`删除成功`),
										icon: 'success'
									});
								})
								.catch(err => {
									return that.$util.Tips({
										title: err
									});
								});
						} else if (res.cancel) {
							return that.$util.Tips({
								title: that.$t(`已取消`)
							});
						}
					}
				});

			}
		},
		onReachBottom: function() {
			this.getOrderList();
		},
		// 滚动监听
		onPageScroll(e) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
			uni.$emit('scroll');
		},
	};
</script>

<style scoped lang="scss">
	.my-order .header {
		height: 200rpx;
		padding: 0 30rpx;
	}

	.my-order .header .picTxt {
		height: 190rpx;
	}

	.my-order .header .picTxt .text {
		color: rgba(255, 255, 255, 0.8);
		font-size: 26rpx;
		font-family: 'Guildford Pro';
	}

	.my-order .header .picTxt .text .name {
		font-size: 34rpx;
		font-weight: bold;
		color: #fff;
		margin-bottom: 20rpx;
	}

	.my-order .header .picTxt .pictrue {
		width: 122rpx;
		height: 109rpx;
	}

	.my-order .header .picTxt .pictrue image {
		width: 100%;
		height: 100%;
	}

	.my-order .nav {
		background-color: #fff;
		width: 690rpx;
		height: 140rpx;
		border-radius: 6rpx;
		margin: -73rpx auto 0 auto;
	}

	.my-order .nav .item {
		text-align: center;
		font-size: 26rpx;
		color: #282828;
		width: 3rem;
		padding: 27rpx 0;
		border-bottom: 5rpx solid transparent;
	}

	.my-order .nav .item.on {
		/* #ifdef H5 || MP */
		font-weight: bold;
		/* #endif */
		/* #ifdef APP-PLUS */
		color: #000;
		/* #endif */
		border-color: var(--view-theme);
	}

	.my-order .nav .item .num {
		margin-top: 18rpx;
	}

	.my-order .list {
		width: 690rpx;
		margin: 14rpx auto 0 auto;
	}

	.my-order .list .item {
		background-color: #fff;
		border-radius: 6rpx;
		margin-bottom: 14rpx;
	}

	.my-order .list .item .title {
		height: 84rpx;
		padding: 0 30rpx;
		border-bottom: 1rpx solid #eee;
		font-size: 28rpx;
		color: #282828;
	}

	.my-order .list .item .title .sign {
		font-size: 24rpx;
		padding: 0 7rpx;
		height: 36rpx;
		margin-right: 15rpx;
	}

	.my-order .list .item .item-info {
		padding: 0 30rpx;
		margin-top: 22rpx;
	}

	.my-order .list .item .item-info .pictrue {
		width: 120rpx;
		height: 120rpx;

		/deep/,
		/deep/image,
		/deep/.easy-loadimage,
		/deep/uni-image {

			width: 120rpx;
			height: 120rpx;
			border-radius: 6rpx;
		}
	}

	.my-order .list .item .item-info .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 6rpx;
	}

	.my-order .list .item .item-info .text {
		width: 486rpx;
		font-size: 28rpx;
		color: #999;
		margin-top: 6rpx;
		display: flex;
		line-height: 39rpx;
	}

	.my-order .list .item .item-info .text .name {
		width: 306rpx;
		color: #282828;
		height: 78rpx;
	}

	.my-order .list .item .item-info .text .money {
		text-align: right;
		flex: 1;
	}

	.my-order .list .item .totalPrice {
		font-size: 26rpx;
		color: #282828;
		text-align: right;
		margin: 27rpx 0 0 30rpx;
		padding: 0 30rpx 30rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.my-order .list .item .totalPrice .money {
		font-size: 28rpx;
		font-weight: bold;
		color: var(--view-priceColor);
	}

	.my-order .list .item .bottom {
		height: 107rpx;
		padding: 0 30rpx;
	}

	.my-order .list .item .bottom .bnt {
		width: 176rpx;
		height: 60rpx;
		text-align: center;
		line-height: 60rpx;
		color: #fff;
		border-radius: 50rpx;
		font-size: 27rpx;
	}

	.my-order .list .item .bottom .bnt.cancelBnt {
		border: 1rpx solid #ddd;
		color: #aaa;
	}

	.my-order .list .item .bottom .bnt~.bnt {
		margin-left: 17rpx;
	}

	.noCart {
		margin-top: 171rpx;
		padding-top: 0.1rpx;
	}

	.noCart .pictrue {
		width: 414rpx;
		height: 336rpx;
		margin: 78rpx auto 56rpx auto;
	}

	.noCart .pictrue image {
		width: 100%;
		height: 100%;
	}

	.my-order .list .item .item-info .text .money .return {
		// color: var(--view-priceColor);
		margin-top: 10rpx;
		font-size: 24rpx;
	}
</style>
